//
//  MainTabBarController.m
//  Movie
//
//  Created by mycomputer on 15/7/17.
//  Copyright (c) 2015年 杨韦智. All rights reserved.
//

#import "MainTabBarController.h"
#import "MovieViewController.h"
#import "NewsViewController.h"
#import "TopViewController.h"
#import "CinemaViewController.h"
#import "MoreViewController.h"
#import "MyButton.h"
#import "BaseNavigationController.h"
#import "BaseViewController.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.width

@interface MainTabBarController ()

@end

@implementation MainTabBarController

- (void)_createSubViews {
    MovieViewController *movieViewController = [[MovieViewController alloc] init];
    NewsViewController *newsViewController = [[NewsViewController alloc] init];
    TopViewController *topViewController = [[TopViewController alloc] init];
    CinemaViewController *cinemaViewController = [[CinemaViewController alloc] init];
    MoreViewController *moreViewController = [[MoreViewController alloc] init];
    NSArray *viewArray = @[movieViewController,newsViewController,topViewController,cinemaViewController,moreViewController];
    
    NSMutableArray *navArray = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i<5; i++) {
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:viewArray[i]];
        [navArray addObject:nav];
    }
    
    self.viewControllers = navArray;
    
}

- (void)_setBabBr {
    
    for (UIView *subView in self.tabBar.subviews) {
        Class cls = NSClassFromString(@"UITabBarButton");
        if ([subView isKindOfClass:cls]) {
            [subView removeFromSuperview];
        }
    }
    
    _selectedView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth/5, 49)];
    _selectedView.image = [UIImage imageNamed:@"selectTabbar_bg_all1"];
    [self.tabBar addSubview:_selectedView];
    
    CGFloat width = CGRectGetWidth(self.tabBar.frame)/5;
    CGFloat height = CGRectGetHeight(self.tabBar.frame);
    NSArray *imageName = @[@"movie_home",@"msg_new",@"start_top250",@"icon_cinema",@"more_setting"];
    NSArray *titleName = @[@"电影",@"新闻",@"Top",@"影院",@"更多"];
    
    for (NSInteger i = 0; i<5; i++) {
        MyButton *button = [[MyButton alloc] initWithFrame:CGRectMake(i*width, 0, width, height) withImageName:imageName[i] withTitle:titleName[i]];
        button.tag = i;
        [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:button];
        
    }
    self.tabBar.backgroundImage = [UIImage imageNamed:@"nav_bg_all"];
    self.tabBar.translucent = YES;
}

- (void)btnAction:(MyButton *)button {
    self.selectedIndex = button.tag;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:.3];
    _selectedView.center = button.center;
    [UIView commitAnimations];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createSubViews];
    [self _setBabBr];
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
